/*
 * @Description: 顶部导航栏
 * @Author: YWJ 
 * @Date: 2022-04-20 18:56:29 
 * @Last Modified by: YWJ
 * @Last Modified time: 2022-05-17 11:02:21
 */

<template>
  <el-header class="me-area">
    <el-row class="me-header">
      <el-col :span="4" class="me-header-left">
        <router-link to="/" class="me-title">
          <img src="/img/logo.png" />
        </router-link>
      </el-col>

      <el-col v-if="!simple" :span="16">
        <el-menu
          :router="true"
          menu-trigger="click"
          active-text-color="#00aaff"
          :default-active="activeIndex"
          mode="horizontal"
        >
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/tag/all">推文分类</el-menu-item>
          <el-menu-item index="/search">搜索推文</el-menu-item>
          <el-menu-item v-if="Logi" index="/personalcenter"
            >个人主页</el-menu-item
          >
          <el-col :span="4" :offset="2">
            <el-menu-item v-if="Logi" index="/write"
              ><i class="el-icon-edit"></i>写推文</el-menu-item
            >
          </el-col>

          <el-col :span="4">
            <el-menu-item v-if="ban" style="color:#E6A23C"
              ><i class="el-icon-warning"></i>已被禁言</el-menu-item
            >
          </el-col>

        </el-menu>
      </el-col>


      <el-col :span="4">
        <el-menu
          :router="true"
          menu-trigger="click"
          mode="horizontal"
          active-text-color="#00aaff"
        >
          <template v-if="!Logi">
            <el-menu-item index="/login">
              <el-button type="text">登录</el-button>
            </el-menu-item>
            <el-menu-item index="/register">
              <el-button type="text">注册</el-button>
            </el-menu-item>
          </template>

          <template v-else>
            <el-submenu index>
              <template slot="title">
                <img class="me-header-picture" :src="user.avatar" />
              </template>
              <el-menu-item index @click="logout"
                ><i class="el-icon-back"></i>退出</el-menu-item
              >
            </el-submenu>
          </template>
        </el-menu>
      </el-col>
    </el-row>
  </el-header>
</template>

<script>
import global from "@/global/global";
export default {
  name: "BaseHeader",
  props: {
    activeIndex: String,
    simple: {
      type: Boolean,
      default: false,
    },
  },
  created() {
    this.user.avatar =
      "http://chm.pengdong.top/index/get/" + localStorage.getItem("avatar");
  },
  data() {
    return {
      user: {
        login: false,
        avatar: "",
      },
    };
  },
  computed: {
    Logi() {
      if (
        localStorage.getItem("account") != "99999999999" 
      )
        return true;
      else return false;
    },
    ban(){
      if (
        localStorage.getItem("account") != "99999999999" &&
        localStorage.getItem("state") == 2
      )
        return true;
      else return false;
    }
  },
  methods: {
    logout() {
      this.$router.push({ path: `/` });
      console.log(this.Logi);
      localStorage.setItem("token", "");
      localStorage.setItem("account", "99999999999");
      localStorage.setItem("uid", "");
      localStorage.setItem("nickname", "");
      localStorage.setItem("state", "");
      localStorage.setItem("password", "");
      localStorage.setItem("createTime", "");
      localStorage.setItem("avatar", "");
      global.account = "";
      location.reload();
      console.log(this.Logi);
    },
  },
};
</script>

<style>
.el-button {
  color: #00aaff;
}

.el-header {
  position: fixed;
  width: 100%;
  z-index: 1024;
  min-width: 100%;
  box-shadow: 0 2px 3px hsla(0, 0%, 7%, 0.1), 0 0 0 1px hsla(0, 0%, 7%, 0.1);
}

.me-title {
  margin-top: 10px;
  font-size: 24px;
}

.me-header-left {
  margin-top: 10px;
}

.me-title img {
  max-height: 2.4rem;
  max-width: 100%;
}

.me-header-picture {
  width: 36px;
  height: 36px;
  border: 1px solid #ddd;
  border-radius: 50%;
  vertical-align: middle;
  background-color: white;
}
</style>